var correctActionChart;
var faultRemoveChart;

$(function() {
	initYear();
});

function initYear() {
	var d = new Date();
	var currentYear = d.getFullYear();
	$("#year").val(currentYear);
	$('#year').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
}
resetYear();
function resetYear() {
	initYear();
	redrawCharts();
}

redrawCharts();
function redrawCharts() {
	var result = eval('('+document.getElementById('toolForm:chartJsonData').value+')');
	genChart(result);
	$("#a_title").html(result[2]);
}


function genChart(data){
	var actionData = eval('('+data[0]+')');
	var faultRemoveData = eval('('+data[1]+')');
	var info = eval('('+data[3]+')');
	if (typeof(actionData.all) == "undefined") {
		$("#a_li").html('全网公司范围内'+info.voltageLevel+'保护正确动作率（NaN）,公司计划完成率（NaN）');
		setChart(null,'CorrectAction');
	} else {
		var reality = "NaN";
		if(actionData.all.reality != "NaN") {
			reality = actionData.all.reality+"%";
		}
		var plan = "NaN";
		if(actionData.all.plan != "NaN") {
			plan = actionData.all.plan+"%";
		}
		$("#a_li").html('全网公司范围内'+info.voltageLevel+'保护正确动作率（'+reality+'）,公司计划完成率（'+plan+'）');
//		$("#a_li").html('全网公司范围内'+info.voltageLevel+'保护正确动作率（'+actionData.all.reality+'%）,公司计划完成率（'+actionData.all.plan+'%）');
		setChart(actionData,'CorrectAction');
	}
	if(typeof(faultRemoveData.all) == "undefined") {
		$("#f_li").html('全网公司范围内'+info.voltageLevel+'故障快速切除率（NaN）,公司计划完成率（NaN）');
		setChart(null,'FaultRemove');
	} else {
		var reality = "NaN";
		if(faultRemoveData.all.reality != "NaN") {
			reality = faultRemoveData.all.reality+"%";
		}
		var plan = "NaN";
		if(faultRemoveData.all.plan != "NaN") {
			plan = faultRemoveData.all.plan+"%";
		}
		$("#f_li").html('全网公司范围内'+info.voltageLevel+'故障快速切除率（'+reality+'）,公司计划完成率（'+plan+'）');
		setChart(faultRemoveData,'FaultRemove');
	}
}

function setChart(data,chartId) {
	if (!data && typeof(data)!="undefined" && data!=0) {
		chart = new NormalRadar(chartId);
		if("FaultRemove"===chartId) {
			$("#FaultRemove").html("该时间暂无数据信息");
		} else if ("CorrectAction"===chartId) {
			$("#CorrectAction").html("该时间暂无数据信息");
		}
	} else {
		if(data.groups.length > 0) {
			var indicators = [];
			var ps = [];
			var rs = [];
			var mi = 100;
			var ma = 100;
			for(var j in data.groups){
				var obj = data.groups[j];
				if("NaN"!=obj.plan) {
					if(obj.plan<mi) {
						mi=obj.plan;
					}
					if(obj.plan>ma) {
						ma = obj.plan;
					}
				}
				if("NaN"!=obj.reality) {
					if(obj.reality<mi) {
						mi=obj.reality;
					}
					if(obj.reality>ma) {
						ma = obj.reality;
					}
				}
			}		
			for(var i in data.groups){
				var obj = data.groups[i];
				if(obj.plan != -1){
					indicators.push({name:obj.name,max:ma,min:mi});
					ps.push(obj.plan);
					rs.push(obj.reality);
				}
			}
			chart = new NormalRadar(chartId);
			chart
			.setLegend(['计划完成率','实际完成率'])
			.setIndicator(indicators)
			.addData({name:'计划完成率',value:ps,itemStyle:{normal:{color:'red'}},label:{normal:{show:true,formatter: '{b}: {c}'}}})
			.addData({name:'实际完成率',value:rs,itemStyle:{normal:{color:'#00a65a'}},label:{normal:{show:true,offset:[50,50],formatter: '{b}: {c}'}}}).show();
		} else {
			chart = new NormalRadar(chartId);
		}
	}
}

function changeYear() {
	var year = $("#year").val();
	selectYear([ {
		name : 'year',
		value : year
	}]);
}

function changeQueryYear(){
	var year = $("#mYear").val();
	selectQueryYear([ {
		name : 'mYear',
		value : year
	}]);
}
function changeQueryYear1(){
	var year = $("#mYear1").val();
	selectQueryYear([ {
		name : 'mYear',
		value : year
	}]);
}
function initQueryYear() {
	var d = new Date();
	var currentYear = d.getFullYear();
	var year = $("#year").val();
	$("#mYear").val(year);
	$('#mYear').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
}
function initQueryYear1() {
	var d = new Date();
	var currentYear = d.getFullYear();
	var year = $("#year").val();
	$("#mYear1").val(year);
	$('#mYear1').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
}